package com.tencent.qf.common.redis.impl;

import com.tencent.qf.common.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class RedisServiceImpl implements RedisService {
    @Resource
    private RedisTemplate redisTemplate = null;

    @PostConstruct
    public void init() {
        this.initRedisTemplate();
    }

    private void initRedisTemplate() {
        RedisSerializer serializer = redisTemplate.getStringSerializer();
        redisTemplate.setKeySerializer(serializer);
        redisTemplate.setValueSerializer(serializer);
    }

    @Override
    public Object getValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void setValue(String key, String value, Long times, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, times, unit);
    }

    @Override
    public void del(String key) {
        redisTemplate.delete(key);
    }
}
